package com.bigstar.lolen_e;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class RuneList extends ListActivity {
	String[] items;
	int type = 0;
	int startIndex = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
        type = Integer.parseInt(getIntent().getExtras().getString("type"));
		if(type >= 1 && type <= 9){
			items = new String[19];
			startIndex = 0;
		}else if(type >= 11 && type <= 19){
			items = new String[22];
			startIndex = 19;
		}else if(type >= 21 && type <= 29){
			items = new String[24];
			startIndex = 19+22;
		}else if(type >= 31 && type <= 33){
			startIndex = 19+22+24;
			items = new String[33];
		}
		setListAdapter(new ListAdapter(this));
		setContentView(R.layout.item_list); 
    }
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }
    
    int position;
	@Override
    public void onListItemClick(ListView parent, View v, int position, long id) {
		this.position = position;
		Drawable drawable = null;
        String path = "rune_img/"+Rune.runes.get(startIndex+position).image_name+".png";
		try {
			drawable = Drawable.createFromStream(getAssets().open(path),"src");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);  
		alt_bld.setMessage("Do you want to apply?").setCancelable(false)
		.setIcon(drawable)
		.setPositiveButton("Apply only one",  
						new DialogInterface.OnClickListener() {  
					public void onClick(DialogInterface dialog, int id) {  
						if(type >= 1 && type <= 9){
							Rune.red[type-1] = RuneList.this.position;
						}else if(type >= 11 && type <= 19){
							Rune.blue[type-11] = startIndex+RuneList.this.position;
						}else if(type >= 21 && type <= 29){
							Rune.yellow[type-21] = startIndex+RuneList.this.position;
						}else if(type >= 31 && type <= 33){
							Rune.big[type-31] = startIndex+RuneList.this.position;
						}
						dialog.cancel();
						RuneList.this.finish();
					}  
				}).setNegativeButton("Close",  
	    		   new DialogInterface.OnClickListener() {  
					public void onClick(DialogInterface dialog, int id) {  
						dialog.cancel();  
					}  
				}).setNeutralButton("Apply All", new DialogInterface.OnClickListener() {  
					public void onClick(DialogInterface dialog, int id) {  
						if(type >= 1 && type <= 9){
							for(int i = 0; i < Rune.red.length; i++){
								Rune.red[i] = RuneList.this.position;
							}
						}else if(type >= 11 && type <= 19){
							for(int i = 0; i < Rune.blue.length; i++){
								Rune.blue[i] = startIndex+RuneList.this.position;
							}
						}else if(type >= 21 && type <= 29){
							for(int i = 0; i < Rune.yellow.length; i++){
								Rune.yellow[i] = startIndex+RuneList.this.position;
							}
						}else if(type >= 31 && type <= 33){
							for(int i = 0; i < Rune.big.length; i++){
								Rune.big[i] = startIndex+RuneList.this.position;
							}
						}
						dialog.cancel(); 
						RuneList.this.finish();
					}
				});
		AlertDialog alert = alt_bld.create();  
		alert.setTitle(Rune.runes.get(startIndex+position).name);  
		alert.show();  
	}
    
	 static class ViewHolder {
		 ImageView item_img;
		 TextView item_name;
		 TextView item_text;
			
		 //TextView tvGoodsLocation;
	 }
	 
	class ListAdapter extends ArrayAdapter{
		Activity context;
		private LayoutInflater mInflater;
		public ListAdapter(Activity context){
			super(context, R.layout.item_info_row, items);
			this.context = context;
			 mInflater = LayoutInflater.from(context);
		}
		     
		public View getView(int position, View convertView, 
				ViewGroup parent){
			ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.item_info_row, null); 
				holder = new ViewHolder();
				holder.item_name = (TextView)convertView.findViewById(R.id.item_name); 
				holder.item_text = (TextView)convertView.findViewById(R.id.item_text); 
				holder.item_img = (ImageView)convertView.findViewById(R.id.item_img); 
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.item_name.setText(Rune.runes.get(startIndex+position).name+"("+Rune.runes.get(startIndex+position).price+"IP)");
			holder.item_text.setText(Rune.runes.get(startIndex+position).text);
			
	        String path = "rune_img/"+Rune.runes.get(startIndex+position).image_name+".png";
			try {
				holder.item_img.setImageDrawable(Drawable.createFromStream(getAssets().open(path),"src"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return convertView;
		}
	}
}
